package classification;

import probability.LogisticFunction;
import basics.Dataset;
import basics.DatasetItem;
import basics.Vector;
import basics.VectorMatrix;

public class LogisticRegression extends Classifier<Integer> {

	private LogisticFunction _logistic;
	private final double eta = .2;

	@Override
	public Integer predict(VectorMatrix x1) {
		Vector x = (Vector) x1;
		double d = _logistic.measure(x);
		return (1. / _logistic.expThetaX(x)) * d > d ? 0 : 1;
	}

	public void update(Vector x, int y) {
		y = y >= .5 ? 1 : y;
		y = y < 0.5 ? 0 : y;
		double p = _logistic.measure(x);
		_logistic.updateParam(x.times(y - p).times(eta));
	}

	@Override
	public void train(Dataset<Vector, Integer> ds) {
		_logistic = new LogisticFunction(ds.feature(0).length());
		for (int i = 0; i < ds.size(); i++) {
			update(ds.feature(i), ds.classVal(i));
		}
	}

}
